<?php

class MsggLinkedsee extends MsggChannelBase
{
    const CONFIG_SERVICE_TOKEN = 'service_token';

    const CALL_URL = 'http://www.linkedsee.com/alarm/cloudchannel';

    const TYPE_SMS = 'sms';
    const TYPE_CALL = 'call';

    const ERROR_CODE_FAIL = 1;

    private static $_conf = array();
    private static $_service_token = 'd77b989004127749da11005c79bdd7b8';

    public static function sms($params = array(), $conf = array())
    {
        $result = array('code' => self::ERROR_CODE_FAIL, 'msg' => 'linxi can\'t send sms');
        return $result;
    }



    public static function call($params = array(), $conf = array())
    {
        static::$_conf = $conf;
        $result = array('code' => 0, 'msg' => '', 'data' => array(), 'ext' => array());
        $receiver = $params['phone'];
        $serviceToekn = self::getConf(self::CONFIG_SERVICE_TOKEN, self::$_service_token);
        $lx_type = 'phone';//Fixed immutable
        $title = 'notice_phone';//Fixed immutable
        $content = 'test通知语音';//Fixed immutable
        try {
            if (empty($serviceToekn)) {
                throw  new Exception('service token is null', self::ERROR_CODE_FAIL);
            }
            if (empty(AlarmPerson::model()->findAllByAttributes(['phone' => $receiver]))) {
                throw  new Exception($receiver . ' phone is not this in linxi tongxunlu ', self::ERROR_CODE_FAIL);
            }
            $url = self::CALL_URL;
            $postdata = array(
                'receiver' => $receiver,
                'title' => $title,
                'type' => $lx_type,
                'content' => $content,
            );
            $headers = array('Servicetoken' => $serviceToekn);
            $resp = self::curlApi($url, json_encode($postdata,JSON_UNESCAPED_UNICODE),array(), $headers);
            $result['data'] = $resp;
            $result['ext']['id'] = self::genExtId($receiver);//gen Md5
            $result['ext']['status'] = MsggCall::STATUS_SUCCESS;
        } catch (Exception $e) {
            //gen result
            $result['code'] = $e->getCode();
            $result['msg'] = $e->getMessage();
            $result['ext']['status'] = MsggCall::STATUS_ERROR;
        }
        //return result
        $result['ext']['class'] = self::class;
        return $result;
    }

    private static function curlApi($url, $postdata = array(), $options = array(), $headers = array())
    {
        try {
            if (empty($headers)) {
                $headers = array();
            }
            $headers['Accept'] = 'application/json;';
            $try = self::getConf('try', 2);
            while ($try) {
                $try--;
                $cres = Curl::post($url, $postdata, $options, $headers);
                if ($cres['err']) {
                    if ($try) {
                        continue;
                    }
                    throw new Exception('CurlErr:' . $cres['err'], self::ERROR_CODE_FAIL);
                }
                if (empty($cres['content'])) {
                    if ($try) {
                        continue;
                    }
                    throw new Exception('response content empty', self::ERROR_CODE_FAIL);
                }
                if (!($resp = json_decode($cres['content'], true))) {
                    if ($try) {
                        continue;
                    }
                    throw new Exception('response content decode fail:' . $cres['content'], self::ERROR_CODE_FAIL);
                }
                $respCode = isset($resp['status']) ? $resp['status'] : null;
                if ($respCode != 'true') {
                    if ($try) {
                        continue;
                    }
                    throw new Exception('response status abnormal:' . $cres['content'], self::ERROR_CODE_FAIL);
                }
                return $resp;
            }
        } catch (Exception $e) {
            throw new Exception($e->getMessage(), self::ERROR_CODE_FAIL);
        }
    }

    public static function getConf($name, $default = null)
    {
        return isset(static::$_conf[$name]) ? static::$_conf[$name] : $default;
    }

    private static function genExtId($phone)
    {
        $time = time();
        return md5($phone . $time);
    }

}
